import { syncSetTimeout, sendMessage, getMessage, dispatch } from "../../global/tools.js";
export async function openCuiji(isAuto) {
    return new Promise(async (resolve, reject) => {
        await syncSetTimeout(50);
        let target = jQuery(".contact-info .right i.icon-cuiji");
        if ((target.length != 0) && jQuery(".ivu-modal-content div.ivu-modal-confirm-head-title:contains('操作失败')").length === 0) {
            if (target.length != 1 && isAuto) {
                let phoneNumber = jQuery("#detail div.caseinfo ul.case-item > li > div.key:contains('债务人手机号')").next("div.value").text().trim();
                target = jQuery(`ul.contacts-list > li:contains('${phoneNumber}')`).find(".contact-info .right i.icon-cuiji");
            } else if (!isAuto) {
                console.error("[aiccyunPlugin]", "anmi > localTools > openCuiji: 打开失败");
                reject();
            }
            dispatch(target, "click");
            await syncSetTimeout(200);
            if (jQuery("div.refund-box > div.refund-title > span:contains('填写催记')").length === 1) {
                resolve();
            } else {
                console.error("[aiccyunPlugin]", "anmi > localTools > openCuiji: 打开失败");
                reject();
            }
        } else {
            console.error("[aiccyunPlugin]", "anmi > localTools > openCuiji: target.length !== 1");
            reject();
        }
    });
}

export async function nextContect() {
    return new Promise(async (resolve, reject) => {
        let nextBtn = jQuery("#content-wrapper div.next-btn.right-btn");
        if (nextBtn.length === 1) {
            nextBtn.click();
            for (let i = 0; i < 10; i++) {
                await syncSetTimeout(500);
                if (jQuery("div.ivu-spin-main > span.ivu-spin-dot").length === 0) {
                    break;
                }
            }
            if (jQuery("div.ivu-spin-main > span.ivu-spin-dot").length === 0) {
                resolve();
            } else {
                console.error("[aiccyunPlugin]", "anmi > localTools > nextContect: 打开失败");
                reject();
            }
        } else {
            console.error("[aiccyunPlugin]", "anmi > localTools > nextContect: target.length !== 1");
        }
    });
}